package com.xja.webchat.controller;

import com.xja.webchat.entity.Chatuser;
import com.xja.webchat.service.ChatUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class WebController {
    private Logger logger = LoggerFactory.getLogger(WebController.class);
    @Autowired
    private ChatUserService chatUserService;

    @PostMapping("user_settouxiang")
    public Map<String,Object> updatetouxiang(@RequestParam("id")Integer id,
                                             @RequestParam("tupian") MultipartFile tupian,HttpServletRequest request
    ) {
        Map<String,Object> resuMap = new HashMap<>();
        try {
            System.out.println("========"+id);
            Chatuser chatuser = chatUserService.selectUserById(id);
            if(tupian.getOriginalFilename()!=null || "".equals(tupian.getOriginalFilename())){
                String oldname = tupian.getOriginalFilename();
                if(oldname!=null && !"".equals(oldname)){
                    String filename = UUID.randomUUID().toString()+oldname.substring(oldname.lastIndexOf("."));
                    String savePath = "D:/Studyuse/idea_workspace/webchat_git_two/webchat_web/src/main/resources/static/source/img/"+filename;
                    tupian.transferTo(new File(savePath));
                    chatuser.setProfilehead(filename);
                }
            }
            chatUserService.updateById(chatuser);
            resuMap.put("status","true");
            return resuMap;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        resuMap.put("status","false");
        return resuMap;
    }

    @PostMapping("user_setting")
    public Map<String,Object> update(@RequestBody Chatuser chatuser) {
        Map<String,Object> resuMap = new HashMap<>();
        try {
            System.out.println("========"+chatuser.getId());
            chatUserService.updateById(chatuser);
            resuMap.put("status","true");
            return resuMap;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        resuMap.put("status","false");
        return resuMap;
    }


    @GetMapping("user")
    public Map<String,Object> selectById(HttpSession session){
        Map<String,Object> resuMap = new HashMap<>();
        try {

            logger.info("跳转到个人信息的功能");
            Chatuser loginUser = (Chatuser)session.getAttribute("loginUser");
            Chatuser chatuser = chatUserService.selectUserById(loginUser.getId());
            resuMap.put("chatuser",chatuser);
            resuMap.put("status","true");
            return resuMap;
        }catch (Exception ex){
            logger.error("有问题了:",ex);
        }
        resuMap.put("status","false");
        return resuMap;
    }


    @PostMapping("login")
    public Map<String,Object> login(@RequestBody Chatuser user, HttpServletRequest request){
        Map<String,Object> resuMap = new HashMap<>();
        try {
            HttpSession session = request.getSession();
            Chatuser loginUser = chatUserService.selectChatuserByuserid(user.getUserid());
            if(loginUser==null){
                resuMap.put("status","false");
                resuMap.put("msg","用户不存在");
                return resuMap;
            }
            if(!loginUser.getPassword().equals(user.getPassword())){
                resuMap.put("status","false");
                resuMap.put("msg","密码错误");
                return resuMap;
            }

            session.setAttribute("loginUser",loginUser);
            resuMap.put("status","true");
            return resuMap;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        resuMap.put("statue","false");
        return resuMap;
    }

    @PostMapping("loginUser")//此方法从session中取出当前登录用户
    public Map<String,Object> loginUser(HttpServletRequest request ){
        Map<String,Object> resuMap = new HashMap<>();
        try {
            HttpSession session = request.getSession();
            Chatuser loginUser = (Chatuser)session.getAttribute("loginUser");

            resuMap.put("loginUser",loginUser);
            return resuMap;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        resuMap.put("status","false");
        return resuMap;
    }

    @PostMapping("reg_adduser")
    public Map<String,Object> reg(@RequestBody Chatuser chatuser){
        Map<String,Object> resuMap = new HashMap<>();
        try{
            chatuser.setChecktype(0);
            chatUserService.addChatuser(chatuser);
            resuMap.put("status","true");
            return resuMap;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        resuMap.put("status","false");
        return resuMap;
    }

    @PostMapping("usermassage")
    public Map<String,Object> usermassage(HttpSession session){
        Map<String,Object> resuMap=new HashMap<>();
        try {
            Chatuser loginUser = (Chatuser)session.getAttribute("loginUser");
            if(loginUser!=null){
                List<Chatuser> userList = chatUserService.selectUserAndWdgs(loginUser.getId());
                resuMap.put("userList",userList);
                resuMap.put("status","true");
                return resuMap;
            }
            resuMap.put("status","null");
            return resuMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        resuMap.put("status","false");
        return resuMap;
    }
}
